package com.es.controller;

import com.alibaba.excel.metadata.Sheet;
import com.alibaba.fastjson.JSONObject;
import com.es.entiy.SearchParam;
import com.es.entiy.TbInvLog;
import com.es.service.ITbInvLogService;
import com.es.service.ITestService;
import com.es.utils.EasyExcelUtils;
import com.es.utils.RestResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author SeniorTong丶
 * @Date 2021/11/19 17:40
 * @Version 1.0
 * @Remark
 */
@RestController
@RequestMapping("/esTest")
public class TbInvLogController {

    @Autowired
    private ITbInvLogService tbInvLogService;


    @Autowired
    private ITestService testService;

    /**
     * 创建索引
     *
     * @return
     */
    @PostMapping("/saveIndex")
    public RestResult saveIndex() {

        tbInvLogService.saveIndex("inv_log_test");
        return RestResult.success();
    }

    /**
     *   校验索引是否存在
     * @return
     */
    @GetMapping("/existIndex")
    public RestResult existIndex() {

        return RestResult.success(tbInvLogService.existIndex("inv_log_test"));
    }

    /**
     *   添加数据
     * @return
     */
    @PostMapping("/saveData")
    public RestResult saveData(){

        TbInvLog invLog = new TbInvLog();
        invLog.setLogId(3L);
        invLog.setGoodsName("菜籽油汽车");
        invLog.setGoodsNo("N003");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = sdf.format(new Date());
        invLog.setOpearateDate(format);
        int result = tbInvLogService.saveData(invLog, "inv_log_test");
        return result > 0 ? RestResult.success() : RestResult.error();
    }

    /**
     *  批量添加数据
     * @return
     */
    @PostMapping("/batchSaveData")
    public RestResult batchSaveData(){

        List<TbInvLog> list = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (int i = 4; i < 10; i++) {
            TbInvLog invLog = new TbInvLog();
            invLog.setLogId((long)i);
            invLog.setGoodsName("菜籽油汽车" + i);
            invLog.setGoodsNo("N00" + i);
            String format = sdf.format(new Date());
            invLog.setOpearateDate(format);
            list.add(invLog);
        }
        //返回 false  表示 没有失败
        boolean result = tbInvLogService.batchSaveData(list, "inv_log_test");

        return !result ? RestResult.success() : RestResult.error();
    }


    /**
     *   修改数据
     * @return
     */
    @PutMapping("/editData")
    public RestResult updateData() {
        TbInvLog invLog = new TbInvLog();
        invLog.setLogId(5L);
        invLog.setGoodsName("花生油汽车");
        invLog.setGoodsNo("N003");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = sdf.format(new Date());
        invLog.setOpearateDate(format);
        int result = tbInvLogService.editData(invLog, "inv_log_test");
        return result > 0 ? RestResult.success() : RestResult.error();
    }

    /**
     *  删除数据
     * @param id
     * @return
     */
    @DeleteMapping("/deleteData/{id}")
    public RestResult deleteData(@PathVariable Long id){

        TbInvLog invLog = new TbInvLog();
        invLog.setLogId(id);
        int result = tbInvLogService.deleteData(invLog, "inv_log_test");

        return result > 0 ? RestResult.success() : RestResult.error();
    }

    /**
     * 根据id 查询
     *
     * @param id
     * @return
     */
    @GetMapping("/getDataId/{id}")
    public RestResult getDataId(@PathVariable String id) {

        SearchParam param = new SearchParam();
        param.setId(id);
        param.setIndex("inv_logs");
        JSONObject json = tbInvLogService.getDataById(param);
        return RestResult.success(JSONObject.toJSONString(json));
    }

    /**
     * 批量查询
     *
     * @return
     */
    @GetMapping("/findList")
    public RestResult findList() {
        SearchParam param = new SearchParam();
        param.setIndex("inv_logs");
        param.setType("_doc");
        param.setPageNum(0);
        param.setPageSize(10000);

        JSONObject json = tbInvLogService.findList(param);
        return RestResult.success(JSONObject.toJSONString(json));
    }

    /**
     *   easyExcel 导出 es 中数据
     * @param request
     * @param response
     */
    @GetMapping("/exportData")
    public void exportData(HttpServletRequest request, HttpServletResponse response) {
        long startTime = System.currentTimeMillis();
//        SearchParam param = new SearchParam();
//        param.setIndex("inv_logs");
//        param.setType("_doc");
//        JSONObject json = tbInvLogService.findList(param);
//
//        // 获取数据集合
//        List<TbInvLog> list = (List<TbInvLog>)json.get("list");

//        String fileName = "test" + System.currentTimeMillis() + ".xlsx";
//        String filePath = "D:/BaiduNetdiskDownload/" + fileName;
//        EasyExcelUtils.writeWithTemplate(filePath,list);

        //多 Sheet 导出 需要传分页参数
        ArrayList<EasyExcelUtils.MultipleSheelPropety> list = new ArrayList<>();
        for(int j = 0; j < 4; j++){
            SearchParam param = new SearchParam();
            param.setIndex("inv_logs");
            param.setType("_doc");
            param.setPageNum(j);
            param.setPageSize(50000);

            // 获取数据集合
            JSONObject json = tbInvLogService.findList(param);
            List<TbInvLog> invLogList = (List<TbInvLog>)json.get("list");

            Sheet sheet = new Sheet(j + 1, 0);
            sheet.setSheetName("sheet" + j);

            EasyExcelUtils.MultipleSheelPropety multipleSheelPropety = new EasyExcelUtils.MultipleSheelPropety();
            multipleSheelPropety.setData(invLogList);
            multipleSheelPropety.setSheet(sheet);

            list.add(multipleSheelPropety);
        }
        long endSelectTime = System.currentTimeMillis();
        BigDecimal decimal = new BigDecimal(endSelectTime - startTime).divide(new BigDecimal(1000));
        System.out.println("查询总计--------消耗时间--------------------------------" + decimal + "s");

        // 输出文件
        String fileName = "Test" + System.currentTimeMillis() + ".xlsx";
        EasyExcelUtils.writeWithMultipleSheelBrowser(response,fileName,list);

        long endOutTime = System.currentTimeMillis();
        BigDecimal decimal1 = new BigDecimal(endOutTime - endSelectTime).divide(new BigDecimal(1000));
        System.out.println("输出文件总计--------时间--------------------------------" + decimal1 + "s");

        long endTime = System.currentTimeMillis();
        BigDecimal decimal2 = new BigDecimal(endTime - startTime).divide(new BigDecimal(1000));
        System.out.println("总计--------访问时间--------------------------------" + decimal2 + "s");
    }


    /**
     *   导出excel
     * @param request
     * @param response
     */
    @GetMapping("/exportDataPoi")
    public void exportDataPoi(HttpServletRequest request, HttpServletResponse response) {

    }


    /**
     *   查询  工单->出库  组合数据
     * @return
     */
    @GetMapping("/combinationData")
    public RestResult listAll() {

        List<Map<String, Object>> listMap = testService.listAll(new SearchParam());
        return RestResult.success(listMap);
    }





}
